package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseTeacherDao;
import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.factory.Factory;

/**
 * 学生业务处理类
 */
public class TeacherService implements ITeacherService {
    //创建Dao对象
    private BaseTeacherDao dao = Factory.getTeacherDao();

    public boolean addTeacher(Teacher teacher) {


        //调用dao方法
        return dao.addTeacher(teacher);

    }

    //判断学号是否灿在
    public boolean isExists(String sId) {
        boolean result = false;
        //dao.isExists(sId);这个时  Dao不干
        Teacher[] teachers = dao.findAllTeachers();
        //判断学号是否存在
        for (int i = 0; i < teachers.length; i++) {

            //teachers[i]
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getSid().equals(sId)) {
                result = true;
                break;
            }

        }
        return result;
    }

    public Teacher[] findAllTeachers() {
        Teacher[] allTeachers = dao.findAllTeachers();
        //标记思想
        boolean flag=false;
        for (int i = 0; i < allTeachers.length; i++) {
            //allTeachers[i]
            if(allTeachers[i]!=null){
                flag=true;
                break;
            }
        }
        if(flag){
            return allTeachers;
        }else{
            return  null;
        }

    }

    public void deleteTeacher(String deleteId) {
        dao.deleteTeacher(deleteId);


    }

    public void updateTeacher(String updateId, Teacher teacher) {
        dao.updateTeacher(updateId,teacher);
    }
    //获取学号所在的索引
    public int getIndex(String sid) {

        var result = -1;
        //dao.isExists(sId);这个时  Dao不干
        Teacher[] teachers = dao.findAllTeachers();
        //判断学号是否存在
        for (int i = 0; i < teachers.length; i++) {

            //teachers[i]
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getSid().equals(sid)) {
                result =i ;
                break;
            }

        }
        return result;
    }
    //通过索引删除指定的学生对象
    public void deleteTeacherByIndex(int index) {

        dao.deleteTeacherByIndex(index);
    }



    // //用在service 查找索引的方式 来删除

}
